<template>
  <div class="flowpath-container">
    <div>
      <div class="mb20">
        <span class="color_494949 ft12 pr5">关键字：</span>
        <el-input
          v-model="searchInfo"
          class="w164 mr10"
          placeholder="请输入关键字"
        />
        <el-button class="settingBtn mr10" type="primary" @click="getList">
          查询
        </el-button>
        <el-button
          v-if="MenuAuths('add')"
          class="settingBtn"
          type="primary"
          @click="addFn"
        >
          新增
        </el-button>
      </div>
      <el-table :data="tableData">
        <!-- <el-table-column label="序号" type="index" width="80" /> -->
        <el-table-column
          label="物资出库名称"
          prop="FullName"
          show-overflow-tooltip
        />
        <el-table-column label="出库单号" prop="OBNo" show-overflow-tooltip />
        <el-table-column label="出库类型">
          <template #default="{ row }">
            <div>{{ row.OBType === '1' ? '委托仓储出库' : '' }}</div>
          </template>
        </el-table-column>
        <el-table-column label="用料单位" prop="POUnit" />
        <el-table-column label="发料库点" prop="WHName" show-overflow-tooltip />
        <el-table-column
          label="发料日期"
          prop="DeliveryDate"
          show-overflow-tooltip
        />
        <el-table-column label="操作" width="250">
          <template #default="{ row }">
            <el-button link size="small" type="primary" @click="examineFn(row)">
              审核
            </el-button>
            <el-button
              v-if="MenuAuths('edit')"
              link
              size="small"
              type="primary"
              @click="detailFn(row)"
            >
              详情
            </el-button>
            <el-button link size="small" type="primary" @click="print(row)">
              打印明细
            </el-button>
            <el-button
              v-if="MenuAuths('delete')"
              link
              size="small"
              type="danger"
              @click="deletedFn(row)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          :current-page="pageNo"
          :layout="layout"
          :page-size="pageSize"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </div>
    </div>
    <el-dialog
      v-model="dialogVisible"
      title="物资出库明细"
      width="1200px"
      @close="closeFn"
    >
      <i v-if="status === 1" class="iconfont icon-shenhezhong iconcommon"></i>
      <i v-if="status === 2" class="iconfont icon-yishenhe iconcommon2"></i>
      <detail
        :agreementlist="agreementList"
        :chileddetail="chileddetail"
        :form="form"
        :fromtype="submitType"
        :pickorderdata="pickOrderDataList"
        @changelist="changeList"
        @deletedchild="deletedchild"
      />
      <template #footer>
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确认</el-button>
      </template>
    </el-dialog>
    <!-- 打印页面 -->
    <el-dialog
      v-model="isPrint"
      title="到货预报明细"
      width="78%"
      @close="isPrint = false"
    >
      <print
        ref="tableRef"
        :chileddetail="chileddetail"
        :form="form"
        fromprint="arrivalConfirm"
      />
      <template #footer>
        <el-button @click="isPrint = false">取 消</el-button>
        <el-button type="primary" @click="submitPrint">打印</el-button>
      </template>
    </el-dialog>
    <wordflow
      :drawershow="isdrawershow"
      :formcode="formCode"
      :formid="formId"
      @handclose="handleClose"
    />
  </div>
</template>

<script>
  import {
    OutboundBillData,
    DelOutboundBill,
    UpdateOutboundBill,
    AddOutboundBill,
    OutboundBillDetail,
    agreePageData,
    pickOrderData,
    startflow,
    getinstance,
  } from '@/api/form'
  import detail from './components/detail'
  import wordflow from '@/views/components/wordflow/index'
  import { MenuAuth } from '~/src/utils/permission'
  import VabPrint from '@/plugins/VabPrint'
  import print from './components/print'
  export default defineComponent({
    name: 'Arrival',
    components: { detail, print, wordflow },
    data() {
      return {
        tableData: [],
        searchInfo: '',
        total: 0,
        pageNo: 1,
        pageSize: 20,
        dialogVisible: false,
        enabled: 0,
        DetailPageData: {},
        chileddetail: [],
        submitType: 'add',
        agreementList: [],
        layout: 'total, sizes, prev, pager, next, jumper',
        deleList: [],
        OBMId: '',
        MenuAuths: '',
        pickOrderDataList: [],
        isPrint: false,
        isdrawershow: false,
        formId: '',
        formCode: 'Wms_OutboundBillMaster',
        status: -1,
        form: {
          Code: '',
          FullName: '',
          OBNo: '',
          OBType: '',
          POUnit: '',
          ProName: '',
          CProName: '',
          CustomNo: '',
          WHName: '',
          LCNo: '',
          CarNum: '',
          Receiver: '',
          ReceiveStatus: '',
          DeliveryDate: '',
          TotalWeight: '',
          TotalSafeAmt: '',
          TotalPackAmt: '',
          TotalCheckAmt: '',
          TotalAmt: '',
          Remark: '',
          SourceBillNo: '',
        },
      }
    },
    created() {
      this.MenuAuths = MenuAuth
      this.getList()
    },
    methods: {
      handleClose() {
        this.isdrawershow = false
      },
      async print(row) {
        this.form = row
        this.OBMId = row.OBMId
        await this.OutboundBillDetail()
        this.isPrint = true
      },
      async submitPrint() {
        await VabPrint(this.$refs.tableRef)
      },
      changeList(val) {
        this.chileddetail = val?.Detail
        this.form = {
          Code: val.Code,
          FullName: val.FullName,
          OBNo: val.OBNo,
          OBType: val.OBType,
          POUnit: val.POUnit,
          ProName: val.ProName,
          CProName: val.CProName,
          CustomNo: val.CustomNo,
          WHName: val.WHName,
          LCNo: val.LCNo,
          CarNum: val.CarNum,
          Receiver: val.Receiver,
          ReceiveStatus: val.ReceiveStatus,
          DeliveryDate: val.DeliveryDate,
          TotalWeight: val.TotalWeight,
          TotalSafeAmt: val.TotalSafeAmt,
          TotalPackAmt: val.TotalPackAmt,
          TotalCheckAmt: val.TotalCheckAmt,
          TotalAmt: val.TotalAmt,
          Remark: val.Remark,
          SourceBillNo: val.PONo,
        }
      },
      async examineFn(row) {
        const params = {
          keyValue: row.OBMId,
          code: 'Wms_OutboundBillMaster', //row.Code,
          formUrl: 'outboundBill',
          flowData: null,
        }
        const data = await startflow(params)
        if (data.code === '200') {
          this.formId = row.OBMId
          // this.isdrawershow = true
          this.$router.push({
            path: '/table/detail',
            query: {
              formId: this.formId,
              formcode: 'Wms_OutboundBillMaster',
            },
          })
        } else {
          this.$message({
            message: data.message,
            type: 'error',
          })
        }
      },
      async getList() {
        const params = {
          pageNo: this.pageNo,
          pageSize: this.pageSize,
          tableName: '',
          sort: '',
          order: '',
          wheres: JSON.stringify([
            { name: 'FullName', value: this.searchInfo, displayType: 'like' },
          ]),
          export: false,
          value: '',
        }
        const data = await OutboundBillData(params)
        this.tableData = data.data.rows
        this.total = data.data.total
        console.log(this.tableData)
      },
      deletedchild(e) {
        this.deleList = e
        console.log(e)
      },
      deletedFn(row) {
        this.$confirm('确认删除该文件吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        })
          .then(() => {
            this.deleted(row)
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消删除',
            })
          })
      },
      async deleted(row) {
        const OBMId = []
        OBMId.push(row.OBMId)
        const params = OBMId
        const data = await DelOutboundBill(params)
        if (data.code == '200') {
          this.$message({
            message: '删除成功',
            type: 'success',
          })
          await this.getList()
        }
      },
      //分页条数切换
      handleSizeChange(val) {
        this.pageSize = val
        this.getList()
      },
      //分页切换
      handleCurrentChange(val) {
        this.pageNo = val
        this.getList()
      },
      async addFn() {
        this.deleList = []
        this.submitType = 'add'
        this.status = -1
        this.form = {
          Code: '',
          FullName: '',
          OBNo: '',
          OBType: '',
          POUnit: '',
          ProName: '',
          CProName: '',
          CustomNo: '',
          WHName: '',
          LCNo: '',
          CarNum: '',
          Receiver: '',
          ReceiveStatus: '',
          DeliveryDate: '',
          TotalWeight: '',
          TotalSafeAmt: '',
          TotalPackAmt: '',
          TotalCheckAmt: '',
          TotalAmt: '',
          Remark: '',
          SourceBillNo: '',
        }
        await this.agreePageData()
        await this.pickOrderDatas()
        this.chileddetail = []
        this.dialogVisible = true
      },
      submit() {
        if (this.status === 1 || this.status === 2) {
          const messages =
            this.status === 1
              ? '单据审核中，不允许修改！'
              : '单据已审核，不允许修改'
          this.$message({
            message: messages,
            type: 'warning',
          })
          return
        }
        if (this.submitType === 'add') {
          this.AddOutboundBill()
        } else {
          this.updataSaveList()
        }
      },
      closeFn() {
        this.enabled = 0
        this.form.WorkFlowID = ''
      },
      async detailFn(row) {
        this.deleList = []
        this.form = row
        this.OBMId = row.OBMId
        this.submitType = 'edit'
        await this.getinstances(row)
        await this.OutboundBillDetail()
        await this.agreePageData()
        await this.pickOrderDatas()
        this.dialogVisible = true
        console.log(this.form)
      },
      async getinstances(row) {
        const params = {
          code: 'Wms_OutboundBillMaster',
          formId: row.OBMId,
        }
        const data = await getinstance(params)
        if (data.code === '200') {
          this.status = data.data?.data?.status
        }
      },
      async OutboundBillDetail() {
        const params = {
          pageNo: 1,
          pageSize: 10,
          total: 0,
          tableName: '',
          sort: '',
          order: '',
          wheres: '',
          export: true,
          value: this.OBMId,
        }
        const data = await OutboundBillDetail(params)
        if (data.master) {
          this.$nextTick(() => {
            this.chileddetail = data.rows
            console.log(this.chileddetail)
          })
        }
        console.log(data)
      },
      async pickOrderDatas() {
        const params = {
          pageNo: this.pageNo,
          pageSize: this.pageSize,
          tableName: '',
          sort: '',
          order: '',
          wheres: JSON.stringify([
            { name: 'FullName', value: this.searchInfo, displayType: 'like' },
          ]),
          export: false,
          value: '',
        }
        const data = await pickOrderData(params)
        this.pickOrderDataList = data.data.rows
      },
      async agreePageData() {
        const params = {
          pageNo: this.pageNo,
          pageSize: this.pageSize,
          tableName: '',
          sort: '',
          order: '',
          wheres: '',
          export: true,
          value: '',
        }
        const data = await agreePageData(params)
        this.agreementList = data.data.rows
      },
      async AddOutboundBill() {
        const params = {
          mainData: this.form,
          detailData: this.chileddetail,
          delKeys: [],
        }
        const data = await AddOutboundBill(params)
        if (data.code == '200') {
          this.getList()
          this.$message({
            message: '保存成功',
            type: 'success',
          })
          this.dialogVisible = false
        }
      },
      async updataSaveList() {
        const params = {
          mainData: this.form,
          detailData: this.chileddetail,
          delKeys: this.deleList,
        }
        const data = await UpdateOutboundBill(params)
        if (data.code == '200') {
          this.getList()
          this.$message({
            message: '保存成功',
            type: 'success',
          })
          this.dialogVisible = false
        }
      },
    },
  })
</script>
<style lang="scss" scoped>
  .flowpath-container {
    .color_494949 {
      color: #494949;
    }
    .w164 {
      width: 164px;
    }
    .mb20 {
      margin-bottom: 20px;
    }
    .mr10 {
      margin-right: 10px;
    }

    .iconcommon {
      position: absolute;
      top: 80px;
      right: 70px;
      font-size: 60px;
      color: blue;
    }
    .iconcommon2 {
      position: absolute;
      top: 80px;
      right: 70px;
      font-size: 60px;
      color: red;
    }
  }
</style>
